package com.sky.minioall.service;

import com.sky.minioall.pojo.MinionUploader;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;
import io.minio.http.Method;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;

@Data
@AllArgsConstructor
@Slf4j
@Component
public class MinioService {

    @Resource
    private MinionUploader minionUploader;

    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    public String upload(byte[] bytes, String objectName) {

        // 创建MinioClient实例。
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minionUploader.getEndpoint())
                .credentials(minionUploader.getAccessKeyId(), minionUploader.getAccessKeySecret())
                .build();
        try {
            // 创建PutObject请求。
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(minionUploader.getBucketName())
                    .object(objectName)
                    .stream(new ByteArrayInputStream(bytes), bytes.length, -1)
                    .build());
        } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            System.out.println("Error occurred while uploading file to Minio: " + e.getMessage());
            return "Error occurred while uploading file to Minio: " + e.getMessage();
        }

        try {

            /*
            // 设置URL永不过期
            int expirationSeconds = Integer.MAX_VALUE;
             */

            // 设置URL过期时间为1小时
            Date expiration = new Date(new Date().getTime() + 3600 * 1000);
            // 将Date转换为Calendar对象
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(expiration);
            // 获取Calendar对象的毫秒值
            long expirationMillis = calendar.getTimeInMillis();
            // 计算从当前时间到过期时间的毫秒数
            int expirationSeconds = (int) ((expirationMillis - System.currentTimeMillis()) / 1000);

            // 生成预签名URL
            String objectUrl = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket(minionUploader.getBucketName())
                            .object(objectName)
                            .expiry(expirationSeconds) // 使用int类型的过期时间
                            .build());
            log.info("图片地址：{}", objectUrl);
            return objectUrl;
        } catch (MinioException e) {
            e.printStackTrace();
            return "";
        } catch (IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}

